﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace PPCodes.ToolBoxPad
{
    public partial class ToolBoxMain : UserControl
    {
        public ToolBoxMain()
        {
            InitializeComponent();
        }

        public ToolBox Toolbox
        {
            get
            {
                return this.toolBox1;
            }
            set
            {
                this.toolBox1 = value;
            }
        }

        private string _configFile;

        /// <summary>
        /// 配置文件路径
        /// </summary>
        public string ConfigFile
        {
            get { return _configFile; }
            set { _configFile = value; }
        }


        private void ToolBoxMain_Load(object sender, EventArgs e)
        {
            LoadToolBox();
        }

        public void LoadToolBox()
        {
            //string fileName = Application.StartupPath + @"Config\SharpDevelopControlLibrary.xml";
            //string fileName = PubConstant.ConfigPath + PubConstant.ToolBoxConfigName;

            _configFile = @"Config\ControlLibrary.xml";
            ComponentLibraryLoader componentLibraryLoader = new ComponentLibraryLoader();
            componentLibraryLoader.LoadToolComponentLibrary(_configFile);
            CleanToolBox();
            foreach (Category category in componentLibraryLoader.Categories)
            {
                if (category.IsEnabled)
                {
                    ToolBoxCategory cate = new ToolBoxCategory 
                    { 
                        ImageIndex = -1, 
                        IsOpen = false, 
                        Name = category.Name, 
                        Parent = null 
                    };

                    ToolBoxItem item = null;
                    //item.Tag = null;
                    //item.Name = "<Pointer>";
                    //item.Parent = null;
                    //cate.Items.Add(item);

                    foreach (ToolComponent component in category.ToolComponents)
                    {
                        if (component.IsEnabled)
                        {
                            item = new ToolBoxItem();
                            System.Drawing.Design.ToolboxItem toolboxItem = new System.Drawing.Design.ToolboxItem();
                            toolboxItem.TypeName = component.FullName;
                            toolboxItem.Bitmap = componentLibraryLoader.GetIcon(component);
                            toolboxItem.DisplayName = component.Name;


                            Assembly asm = component.LoadAssembly();
                            toolboxItem.AssemblyName = asm.GetName();

                            //判断控件种类（Dev,Win,Web）

                            if (toolboxItem.TypeName.Split('.')[0].Equals("DevExpress"))
                            {
                                toolboxItem.Description = "Dev";
                            }
                            else
                            {
                                toolboxItem.Description = "Win";
                            }
                            item.Assembly = toolboxItem.AssemblyName.FullName;
                            item.Namespace = toolboxItem.TypeName;
                            item.Image = toolboxItem.Bitmap;
                            item.Tag = toolboxItem;
                            item.Name = component.Name;
                            item.Parent = null;
                            item.Enabled = true;
                            cate.Items.Add(item);
                        }
                    }
                    toolBox1.Categories.Add(cate);
                }
            }
        }

        private void CleanToolBox()
        {
            toolBox1.Categories.Clear();
        }

        private void configraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (ConfigureToolBox cf = new ConfigureToolBox(this))
            {
                cf.ShowDialog();
            }
        }

        private void tsmiBColor_Click(object sender, EventArgs e)
        {
            using (ColorDialog cd = new ColorDialog())
            {
                if (cd.ShowDialog() == DialogResult.OK)
                    Toolbox.CategoryBackColor = cd.Color;
            }
        }

        private void tsmiFColor_Click(object sender, EventArgs e)
        {
            using (ColorDialog cd = new ColorDialog())
            {
                if (cd.ShowDialog() == DialogResult.OK)
                    Toolbox.CategoryFontColor = cd.Color;
            }
        }
    }
}
